package com.ding.dailyup.util;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.ding.dailyup.constant.StockConstant;
import com.ding.dailyup.entity.dto.StockDTO;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

public class StockUtil {

	private static final String SH = "sh";
	private static final String SZ = "sz";

	private static final List<String> SH_PREFIX = Lists.newArrayList("60",
			"68");
	private static final List<String> SZ_PREFIX = Lists.newArrayList("00", "30",
			"39");

	/**
	 * 获取股票code with 交易所的前缀
	 * @param simpleCode
	 * @return
	 */
	public static String getStockCodeWithExchange(String simpleCode) {
		if (StrUtil.isEmpty(simpleCode) || simpleCode.length() < 2) {
			return "";
		}
		String prefix = simpleCode.substring(0, 2);
		if (SH_PREFIX.contains(prefix)) {
			return SH + simpleCode;
		} else if (SZ_PREFIX.contains(prefix)) {
			return SZ + simpleCode;
		}
		return "";
	}

	/**
	 * 查看涨跌图
	 *
	 * @param simpleCode
	 * @param imgLevel
	 *            min daily weekly monthly
	 * @return
	 */
	public static String getRaiseDownImgUrl(String simpleCode, String imgLevel,
			long timestamp) {
		if (StrUtil.isEmpty(simpleCode) || simpleCode.length() < 2) {
			return "";
		}
		String stockCodeWithExchange = getStockCodeWithExchange(simpleCode);
		return String.format(StockConstant.URL_SINA_CHART_CN_FORMATTER, StockConstant.URL_SINA_CHART,
				imgLevel, stockCodeWithExchange, timestamp);
	}

	/**
	 *
	 * @param code
	 * @return 股票名称,当前价格
	 */
	public static Pair<String, String> getStockPriceWithSimpleCode(
			String code) {
		String stockCodeWithExchange = getStockCodeWithExchange(code);
		String resp = HttpUtil
				.get(StockConstant.QT_STOCK_URL + stockCodeWithExchange);
		String assertion = String.format("(?<=v_%s=\").*?(?=\";)",
				stockCodeWithExchange);
		Pattern pattern = Pattern.compile(assertion);
		Matcher matcher = pattern.matcher(resp);
		if (matcher.find()) {
			String[] split = matcher.group().split("~");
			Pair<String, String> pair = new Pair<>(split[1], split[3]);
			return pair;
		}
		return new Pair<String, String>("", "");
	}

	public static Map<String, StockDTO> getStockPriceWithSimpleCode(
			Collection<String> codes) {
		List<String> codesWithHouse = codes.stream()
				.map(StockUtil::getStockCodeWithExchange)
				.collect(Collectors.toList());
		String param = Joiner.on(",").join(codesWithHouse);
		String resp = HttpUtil.get(StockConstant.QT_STOCK_URL + param);
		String[] raws = resp.split("\n");

		Map<String, StockDTO> rs = Maps.newHashMap();
		if (codes.size() != raws.length) {
			return rs;
		}
		for (int i = 0; i < codesWithHouse.size(); i++) {
			String symbol = codesWithHouse.get(i);
			String raw = raws[i];
			String assertion = String.format("(?<=v_%s=\").*?(?=\";)", symbol);
			Pattern pattern = Pattern.compile(assertion);
			Matcher matcher = pattern.matcher(raw);
			while (matcher.find()) {
				String[] metas = matcher.group().split("~");
				StockDTO stock = new StockDTO();

				stock.setStockCode(metas[2]);
				stock.setStockName(metas[1]);
				stock.setLatest(metas[3]);
				stock.setPriceOpen(metas[5]);
				stock.setPriceClose(metas[3]);
				stock.setHigh(metas[33]);
				stock.setLow(metas[34]);

				stock.setPrivateChange(metas[31]);
				stock.setChangeRatio(metas[32]);
				stock.setVolume(metas[36]);
				stock.setTurnover(metas[37]);
				stock.setMarketValue(metas[45]);
				rs.put(metas[2], stock);
			}
		}
		return rs;
	}
}
